<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<TableSearch :search="state.tableData.search" @search="onSearch" @download="onDownload" />
			<Table ref="tableRef" v-bind="state.tableData" class="table-demo" @pageChange="onTablePageChange"
				@sortHeader="onSortHeader" />

		</div>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import table2excel from 'js-table2excel';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { useAttendDailyExportApi } from '/@/api/attend/daily';
import { usePosChargePageApi } from '/@/api/pos/charge';
import { EmptyObjectType, RefType } from '/@/types/global';

// 引入组件
const Table = defineAsyncComponent(() => import('/@/views/pos/charge/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/noAddSearch.vue'));
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 表头内容（必传，注意格式）
		header: [
			{ key: 'employeeCode', colWidth: '', title: '工号', type: 'text', isCheck: true, sortable: true },
			{ key: 'employeeName', colWidth: '', title: '姓名', type: 'text', isCheck: true, sortable: true },
			{ key: 'walletCategoryName', colWidth: '', title: '钱包类型', type: 'text', isCheck: true, sortable: true },
			{ key: 'creaseType', colWidth: '', title: '消费类型', type: 'text', isCheck: true, sortable: true },
			{ key: 'moneyType', colWidth: '', title: '资金类型', type: 'text', isCheck: true, sortable: true },
			{ key: 'volume', colWidth: '', title: '额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'optionType', colWidth: '', title: '操作类型', type: 'text', isCheck: true, sortable: true },
			// { key: 'remark', colWidth: '', title: '备注', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeCashTotal', colWidth: '', title: '变动前 现金次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeATlowanceTotal', colWidth: '', title: '变动前 津贴次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeSubsidyTotal', colWidth: '', title: '变动前 补贴次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeCashAmount', colWidth: '', title: '变动前 现金额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeATTowanceAmount', colWidth: '', title: '变动前 津贴额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'beforeSubsidyAmount', colWidth: '', title: '变动前 补贴额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterCashTotal', colWidth: '', title: '变动后 现金次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterAllowanceTotal', colWidth: '', title: '变动后 津贴次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterSubsidyTotal', colWidth: '', title: '变动后 补贴次数', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterCashAmount', colWidth: '', title: '变动后 现金额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterAlTowanceAmount', colWidth: '', title: '变动后 津贴额度', type: 'text', isCheck: true, sortable: true },
			{ key: 'afterSubsidyAmount', colWidth: '', title: '变动后 补贴额度', type: 'text', isCheck: true, sortable: true },
			// <el-tag :type="scope.row.status === true ? 'success' : 'danger'">{{ scope.row.status === true ? '已关联' :'未关联' }}</el-tag>
		],
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isBorder: true, // 是否显示表格边框
			isSerialNo: false, // 是否显示表格序号
			isSelection: false, // 是否显示表格多选
			isOperate: false, // 是否显示表格操作栏
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '工号', prop: 'employeeCode', placeholder: '请输入工号', required: false, type: 'input', defaultValue: '' },
			{ label: '姓名', prop: 'employeeName', placeholder: '请输入姓名', required: false, type: 'input', defaultValue: '' },
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			pageNum: 1,
			pageSize: 10,
		},
		// 打印标题
		printName: '打印示例',
		downloadList: [] as any,
		optionType: {
			1: '消费',
			2: '退款',
			3: '充值',
			4: '扣款',
			5: '发放',
			6: '清零',
			7: '纠错',
		} as any
	},
});


// 初始化列表数据
const getTableData = () => {
	state.tableData.config.loading = true;
	state.tableData.data = [];
	usePosChargePageApi(state.tableData.param).then(({ content }) => {
		state.tableData.data = content.list.map((item: any) => {
			return {
				...item,
				...item.change,
				creaseType: item.creaseType === 1 ? '增加' : '减少',
				optionType: state.tableData.optionType[item.optionType],
				moneyType: item.moneyType === 1 ? '现金' : (item.moneyType === 2 ? '津贴' : '补贴'),
			}
		}) || []
		console.log(state.tableData.data)
		state.tableData.param.pageSize = content.pageSize
		state.tableData.param.pageNum = content.pageNum
		state.tableData.config.total = content.total
	})
	// 数据总数（模拟，真实从接口取）
	state.tableData.config.loading = false;
};

// 导出明细
const getDownloadData = () => {
	ElMessageBox.confirm(`此操作将导出考勤明细，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		useAttendDailyExportApi(state.tableData.param).then(({ content }) => {
			state.tableData.config.loading = false;
			state.tableData.downloadList = content.map((item: any) => {
				return {
					...item,
					lossSignIn: item.lossSignIn === 1 ? '是' : '否',
					lossSignOut: item.lossSignOut === 1 ? '是' : '否',
					lossWork: item.lossWork === 1 ? '旷工' : '正常',
					status: item.status === 1 ? '异常' : '正常',
				}
			})
			table2excel(state.tableData.header, state.tableData.downloadList, `${themeConfig.value.globalTitle} ${new Date().toLocaleString()}`);
			ElMessage.success('操作成功')
		})
	});
};

// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	// tableRef.value.pageReset();
	getTableData()
};

// 搜索点击时表单回调
const onDownload = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	getDownloadData()
};

// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
	state.tableData.param.pageNum = page.pageNum;
	state.tableData.param.pageSize = page.pageSize;
	getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
	state.tableData.header = data;
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;

		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}

.menu-left {
	height: 650px;
	margin-right: 15px;
	overflow-y: auto;
	padding: 0 5px 30px 0;
	min-width: 300px;
}
</style>
